package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class GenericDaoImpl<T> implements GenericDao<T> {

	@Autowired
	protected SessionFactory sessionFactory;

	public void save(T entity) {
		sessionFactory.getCurrentSession().save(entity);
	}

	public void update(T entity) {
		sessionFactory.getCurrentSession().merge(entity);
		sessionFactory.getCurrentSession().flush();
	}

	public void delete(T entity) {
		sessionFactory.getCurrentSession().delete(entity);

	}

	public T getById(Class clazz, Integer id) {
		return (T) sessionFactory.getCurrentSession().get(clazz, id);
	}

	public List<T> getAll(Class clazz) {
		Query query = sessionFactory.getCurrentSession().createQuery("from " + clazz.getCanonicalName());
		return query.list();
	}

}